#pragma once
#include <conio.h>
#include <iostream>
#include <mutex>
#include "Console.h"
#include <deque>
#include <thread>
#include <string>
using namespace std;

mutex mutex_cout;
deque<char> player1Keys;
deque<char> player2Keys;
class position
{
public:
	int posX, posY;
	int prevX, prevY;
	string player;
	position()
	{

	}
	position(int posX, int posY, int prevX, int prevY, string player)
	{
		this->posX = posX;
		this->posY = posY;
		this->prevX = prevX;
		this->prevY = prevY;
		this->player = player;
	}
};

deque<position> movesDeque;

class Game
{
private:
	char move;
	string gameArray[10][10];
	
public:
	bool gameOver = false;
	Game()
	{
		for (size_t i = 0; i < 10; i++)
		{
			for (size_t j = 0; j < 10; j++)
			{
				gameArray[i][j] = " ";
			}
		}
	}
	~Game()
	{
	}
	void printGameBoard()
	{
		if (movesDeque.empty())
		{
			
		}
		else
		{
			Course::Console::clrscr();
			cout << "|---|---|---|---|---|---|---|---|---|---|" << endl;
			for (size_t i = 0; i < 10; i++)
			{
				
				for (size_t j = 0; j < 10; j++)
				{
					if (j == 0)
					{
						cout << "| " << gameArray[i][j];
					}
					else if (j == 9)
					{
						cout << " |" << gameArray[i][j] << "  |"<< endl << "|---|---|---|---|---|---|---|---|---|---|" << endl;
					}
					else
					{
						cout << " | " << gameArray[i][j];
					}
				}
			}
			position p = movesDeque.front();
			Course::Console::gotoxy(p.posX * 4 + 2, p.posY * 2 + 1);
			cout << p.player;
			movesDeque.pop_front();
		}
	}
	
	void operator()()
	{
		while (1)
		{
			if (_kbhit())
			{
				move = _getch();
				if (move == 'q')
				{
					break;
				}
				else
				{
					mutex_cout.lock();
					if (move == 'w' || move == 'a' || move == 's' || move == 'd')
					{
						player1Keys.push_front(move);
					}
					else if (move == 'i' || move == 'j' || move == 'k' || move == 'l')
					{
						player2Keys.push_front(move);
					}
					mutex_cout.unlock();
				}
			}
			mutex_cout.lock();
			printGameBoard();
			mutex_cout.unlock();
			this_thread::sleep_for(chrono::milliseconds(10));
		}
		
	}
};

